package com.troll.wanandroid.design.activity

import android.view.MenuItem
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.troll.base.BaseSimpleActivity
import com.troll.eth.ex.bindView
import com.troll.wanandroid.BottomNavigationViewHelper
import com.troll.wanandroid.R
import com.troll.wanandroid.design.fragment.ViewPagerAdapter
import com.troll.wanandroid.design.fragment.*


class MainActivity : BaseSimpleActivity() {

    private var menuItem: MenuItem? = null

    private val vp by bindView<ViewPager>(R.id.main_vp)
    private val bottomNav by bindView<BottomNavigationView>(R.id.main_bottom_navigation)

    override val layoutId: Int
        get() = R.layout.activity_main

    override fun flowOfData() {
        BottomNavigationViewHelper.disableShiftMode(bottomNav)
        bottomNav.setOnNavigationItemSelectedListener { it ->
            when (it.itemId) {
                R.id.item_news -> vp.currentItem = 0
                R.id.item_lib -> vp.currentItem = 1
                else -> vp.currentItem = 2
            }
            false
        }
        val adapter =
            ViewPagerAdapter(
                supportFragmentManager
            )
        adapter.addFragment(HomeFragment())
        adapter.addFragment(QuestionFragment())
        adapter.addFragment(SystemFragment())
        vp.adapter = adapter
    }

    override fun flowOfSetup() {
        vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                if (menuItem != null) {
                    menuItem?.isChecked = false
                } else {
                    bottomNav.menu.getItem(0).isChecked = false
                }
                menuItem = bottomNav.menu.getItem(position)
                menuItem?.isChecked = true
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })


    }


}